package band.wukong.practice.springcloud.quickstart.qk14;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wukong(wukonggg@139.com)
 */
@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class Qk14ConsulClientApplication {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/dc")
    public String dc() {
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }

    @RequestMapping("/hi")
    public String home() {
        return "Hi, I'm Qk14ConsulClientApplication.";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Qk14ConsulClientApplication.class).web(true).run(args);
    }

    /*
        TODO WK 现在consul会报app的状态为critical，查看/health报503
        关键内容："error": "java.lang.IllegalArgumentException: Value must not be null"

        SEE: https://github.com/spring-cloud/spring-cloud-consul/issues/365

        health的response如下：
        // 20171113144906
        // http://localhost:9014/health
        {
          "status": "DOWN",
          "discoveryComposite": {
            "description": "Spring Cloud Consul Discovery Client",
            "status": "UP",
            "discoveryClient": {
              "description": "Spring Cloud Consul Discovery Client",
              "status": "UP",
              "services": Array[2][
                "consul",
                "qk-14-consul"
              ]
            }
          },
          "diskSpace": {
            "status": "UP",
            "total": 322126639104,
            "free": 199548248064,
            "threshold": 10485760
          },
          "refreshScope": {
            "status": "UP"
          },
          "consul": {
            "status": "DOWN",
            "services": {
              "consul": Array[0][

              ],
              "qk-14-consul": Array[0][

              ]
            },
            "error": "java.lang.IllegalArgumentException: Value must not be null"
          },
          "hystrix": {
            "status": "UP"
          }
        }

     */
}

